package br.com.sistemasaude.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import br.com.sistemasaude.bean.Usuario;
import br.com.sistemasaude.util.JpaUtil;

public class LoginDAOImpl implements LoginDAO{
	
private final GenericDAO<Usuario> dao;	
	
EntityManager em = new JpaUtil().getEntityManager();

public LoginDAOImpl() {
	dao = new GenericDAO<Usuario>(em, Usuario.class);
	}

public Usuario validaLogin(String login, String senha){
		
		
		
		EntityTransaction t = em.getTransaction();
		
		Usuario user = null;
		
		try{
			t.begin();
			Query q = em.createQuery("select u from Usuario u where u.login =:pLogin and u.senha =:pSenha");
			q.setParameter("pLogin",login);
			q.setParameter("pSenha", senha);
			user = (Usuario) q.getSingleResult();
			t.commit();
					
		}catch (Exception e) {

			e.printStackTrace();
			if (t.isActive()) t.rollback();

		} finally {

			em.close();
		}	
		
		return user;
		
	}

}
